define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wu/workorder/index',
                    add_url: 'wu/workorder/add',
                    //edit_url: 'wu/workorder/edit',
                    del_url: 'wu/workorder/del',
                    multi_url: 'wu/workorder/multi',
                    table: 'wu_workorder',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},

                        {field: 'id', title: __('Id'), operate:false, sortable: true},
                        {field: 'orderdate', title: __('Orderdate'), operate:'RANGE', addclass:'datetimerange', sortable: true},

                        {field: 'spectype.name', title: __('Spectype.name'), sortable: true, searchList: $.getJSON('wu/workorder/searchlist'), formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name'), sortable: true, operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'lbnumber', title: __('Lbnumber'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {
                            field: 'ordernumber',
                            title: __('Ordernumber'),
                            operate: 'LIKE %...%',
                            sortable: true,
                            placeholder: '模糊搜索，*表示任意字符'
                        },
                        {
                            field: 'ProductName',
                            title: __('Productname'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索，*表示任意字符'
                        },
                        {field: 'num', title: __('Num'), operate: false},


                        //{field: 'productspec.name', title: __('Productspec.name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'productspec.specnumber', title: __('Productspec.specnumber'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'startdate', title: __('Startdate'), operate:false, addclass:'datetimerange'},
                        //{field: 'finishdate', title: __('Finishdate'), operate:false, addclass:'datetimerange'},
                        {field: 'lystate', title: __('Lystate'), searchList: {"0":__('Lystate 0'),"1":__('Lystate 1')}, formatter: Table.api.formatter.normal},

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,

                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    title: __('製令單工作詳情'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-newspaper-o',
                                    url: 'wu/planlog/detail2'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                var myColumns = [];
                $.each(table.bootstrapTable('getHiddenColumns'), function (i, item) {
                    myColumns.push(item.field);
                });
                $.cookie('fa_wu_workorder', JSON.stringify(myColumns), {expires: 365, path: '/'});
            });
            if ($.cookie('fa_wu_workorder')) {
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_wu_workorder')), function (i, item) {
                    table.bootstrapTable('hideColumn', item);
                });
            }

            // 批量列印
            $(document).on("click", ".btn-selected", function () {
                var ids = Table.api.selectedids(table);
                var url = 'wu/workorder/lieyin?ids=' + ids;

                var that = this;
                Layer.confirm(__('确定批量列印?'), {icon: 3, title: '提示'}, function (index) {
                    Backend.api.ajax({
                        url: url,
                    }, function () {
                        Layer.close(index);
                        $(".btn-refresh").trigger("click");
                    });
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});